/*
 * Copyright (c) 2023 vitasystems GmbH
 *
 * This file is part of project EHRbase Migration Tool
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ehrbase.migration.exporter.v0.jooq.pg.tables;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import org.ehrbase.migration.exporter.v0.jooq.pg.Ehr;
import org.ehrbase.migration.exporter.v0.jooq.pg.Keys;
import org.ehrbase.migration.exporter.v0.jooq.pg.tables.records.TenantRecord;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Function4;
import org.jooq.JSON;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Records;
import org.jooq.Row4;
import org.jooq.Schema;
import org.jooq.SelectField;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableOptions;
import org.jooq.UniqueKey;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.jooq.impl.TableImpl;

/**
 * This class is generated by jOOQ.
 */
@SuppressWarnings({"all", "unchecked", "rawtypes"})
public class Tenant extends TableImpl<TenantRecord> {

    private static final long serialVersionUID = 1L;

    /**
     * The reference instance of <code>ehr.tenant</code>
     */
    public static final Tenant TENANT = new Tenant();

    /**
     * The class holding records for this type
     */
    @Override
    public Class<TenantRecord> getRecordType() {
        return TenantRecord.class;
    }

    /**
     * The column <code>ehr.tenant.tenant_id</code>.
     */
    public final TableField<TenantRecord, String> TENANT_ID =
            createField(DSL.name("tenant_id"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.tenant.tenant_name</code>.
     */
    public final TableField<TenantRecord, String> TENANT_NAME =
            createField(DSL.name("tenant_name"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.tenant.tenant_properties</code>.
     */
    public final TableField<TenantRecord, JSON> TENANT_PROPERTIES =
            createField(DSL.name("tenant_properties"), SQLDataType.JSON, this, "");

    /**
     * The column <code>ehr.tenant.id</code>.
     */
    public final TableField<TenantRecord, Short> ID = createField(
            DSL.name("id"),
            SQLDataType.SMALLINT
                    .nullable(false)
                    .defaultValue(DSL.field(DSL.raw("ehr.next_sys_tenant()"), SQLDataType.SMALLINT)),
            this,
            "");

    private Tenant(Name alias, Table<TenantRecord> aliased) {
        this(alias, aliased, null);
    }

    private Tenant(Name alias, Table<TenantRecord> aliased, Field<?>[] parameters) {
        super(alias, null, aliased, parameters, DSL.comment(""), TableOptions.table());
    }

    /**
     * Create an aliased <code>ehr.tenant</code> table reference
     */
    public Tenant(String alias) {
        this(DSL.name(alias), TENANT);
    }

    /**
     * Create an aliased <code>ehr.tenant</code> table reference
     */
    public Tenant(Name alias) {
        this(alias, TENANT);
    }

    /**
     * Create a <code>ehr.tenant</code> table reference
     */
    public Tenant() {
        this(DSL.name("tenant"), null);
    }

    public <O extends Record> Tenant(Table<O> child, ForeignKey<O, TenantRecord> key) {
        super(child, key, TENANT);
    }

    @Override
    public Schema getSchema() {
        return aliased() ? null : Ehr.EHR;
    }

    @Override
    public UniqueKey<TenantRecord> getPrimaryKey() {
        return Keys.TENANT_PKEY;
    }

    @Override
    public List<UniqueKey<TenantRecord>> getUniqueKeys() {
        return Arrays.asList(Keys.TENANT_TENANT_ID_KEY, Keys.TENANT_TENANT_NAME_KEY);
    }

    @Override
    public Tenant as(String alias) {
        return new Tenant(DSL.name(alias), this);
    }

    @Override
    public Tenant as(Name alias) {
        return new Tenant(alias, this);
    }

    @Override
    public Tenant as(Table<?> alias) {
        return new Tenant(alias.getQualifiedName(), this);
    }

    /**
     * Rename this table
     */
    @Override
    public Tenant rename(String name) {
        return new Tenant(DSL.name(name), null);
    }

    /**
     * Rename this table
     */
    @Override
    public Tenant rename(Name name) {
        return new Tenant(name, null);
    }

    /**
     * Rename this table
     */
    @Override
    public Tenant rename(Table<?> name) {
        return new Tenant(name.getQualifiedName(), null);
    }

    // -------------------------------------------------------------------------
    // Row4 type methods
    // -------------------------------------------------------------------------

    @Override
    public Row4<String, String, JSON, Short> fieldsRow() {
        return (Row4) super.fieldsRow();
    }

    /**
     * Convenience mapping calling {@link SelectField#convertFrom(Function)}.
     */
    public <U> SelectField<U> mapping(
            Function4<? super String, ? super String, ? super JSON, ? super Short, ? extends U> from) {
        return convertFrom(Records.mapping(from));
    }

    /**
     * Convenience mapping calling {@link SelectField#convertFrom(Class,
     * Function)}.
     */
    public <U> SelectField<U> mapping(
            Class<U> toType, Function4<? super String, ? super String, ? super JSON, ? super Short, ? extends U> from) {
        return convertFrom(toType, Records.mapping(from));
    }
}
